﻿using System;
using System.Data.SQLite;
using System.Configuration;
using System.Windows.Forms;

namespace Koonsoft.Relocate.Winform
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            if (TxtUserAccount.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入用户名！","提示");
                return;
            }

            if (TxtUserPassword.Text.Trim().Equals(string.Empty))
            {
                MessageBox.Show("请输入密码！", "提示");
                return;
            }

            string sql = "select password from backout_user where [user]='" + TxtUserAccount.Text.Trim() + "'";

            SQLiteConnection sqlconn = new SQLiteConnection();
            sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["Koonsoft.Relocate.Winform.Properties.Settings.relocateConnectionString"].ToString();

            SQLiteCommand sqlcmd = new SQLiteCommand();
            sqlcmd.Connection = sqlconn;
            sqlcmd.CommandText = sql;

            try
            {
                sqlcmd.Connection.Open();
                SQLiteDataReader sqlrd = sqlcmd.ExecuteReader();

                if (sqlrd.HasRows)
                {
                    sqlrd.Read();

                    if (sqlrd["password"].ToString().Equals(TxtUserPassword.Text.Trim()))
                    {
                        DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("密码错误！", "失败");
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("用户名不存在！", "失败");
                    return;
                }
            }
            finally
            { 
                sqlcmd.Connection.Close();
            }
            

        }
    }
}
